package com.san.system.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.base.controller.BaseController;
import com.san.common.annotation.Log;
import com.san.common.utils.R;
import com.san.system.domain.RoleDO;
import com.san.system.service.RoleService;

@RequestMapping("/sys/role")
@Controller
public class RoleController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

	String prefix = "system/role";
	@Autowired
	RoleService roleService;

	@RequiresPermissions("sys:role:view")
	@GetMapping()
	String role() {
		return prefix + "/view";
	}

	@RequiresPermissions("sys:role:view")
	@GetMapping("/list")
	@ResponseBody()
	List<RoleDO> list() {
		List<RoleDO> roles = roleService.list();
		return roles;
	}

	@Log("添加角色")
	@RequiresPermissions("sys:role:add")
	@GetMapping("/add")
	String add() {
		return prefix + "/add";
	}

	@Log("编辑角色")
	@RequiresPermissions("sys:role:edit")
	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id, Model model) {
		try {
			RoleDO roleDO = (RoleDO) roleService.getById(id);
			model.addAttribute("role", roleDO);
		} catch (Exception e) {
			logger.error("获取角色失败", e);
		}

		return prefix + "/edit";
	}

	@Log("保存角色")
	@RequiresPermissions("sys:role:add")
	@PostMapping("/save")
	@ResponseBody()
	R save(RoleDO role) {
		try {
			if (roleService.save(role) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("新增角色失败", e);
		}
		return R.error(1, "保存失败");
	}

	@Log("更新角色")
	@RequiresPermissions("sys:role:edit")
	@PostMapping("/doEdit")
	@ResponseBody()
	R doEdit(RoleDO role) {
		try {
			if (roleService.doEdit(role) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("更新角色失败", e);
		}

		return R.error(1, "保存失败");
	}

	@Log("删除角色")
	@RequiresPermissions("sys:role:remove")
	@PostMapping("/remove")
	@ResponseBody()
	R remove(Long id) {
		try {
			if (roleService.remove(id) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("删除角色失败", e);
		}
		return R.error(1, "删除失败");
	}

	@RequiresPermissions("sys:role:batchRemove")
	@Log("批量删除角色")
	@PostMapping("/batchRemove")
	@ResponseBody
	R batchRemove(@RequestParam("ids[]") Long[] ids) {
		try {
			int r = roleService.batchRemove(ids);
			if (r > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("批量删除角色失败", e);
		}

		return R.error();
	}
}
